<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="房间号" prop="room_no">
        <el-input v-model.trim="form.room_no" placeholder="请输入房间号" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { addHouse } from '@/api/room'
  export default {
    name: 'AddRoom',
    data() {
      return {
        form: {
          room_no: '',
          room_type: '2',
        },
        rules: {
          room_no: [
            { required: true, trigger: 'blur', message: '请输入房间号' },
          ],
        },
        title: '添加房间',
        dialogFormVisible: false,
      }
    },
    methods: {
      async showEdit(row) {
        this.form.room_type = row.room_type
        if (this.form.room_type == '2') {
          this.form.home_id = row.home_id
          this.form.community_id = row.community_id
          this.form.group_id = row.group_id
        } else if (this.form.room_type == '3') {
          this.form.building_id = row.building_id
          this.form.floor_id = row.floor_id
          this.form.group_id = row.group_id
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { data } = await addHouse(this.form)
            if (data.is_success) {
              this.$baseMessage(
                '添加成功',
                'success',
                'vab-hey-message-success'
              )
              this.$emit('fetch-data')
              this.close()
            }
          }
        })
      },
    },
  }
</script>
